//
// The resource management layer has been upgraded, thus deprecating this file.
//
#error *** resmgr.cpp has been deprecated. ***

#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#include "lib.h"
#include "stream.h"

#include "resmgr.h"
#include "resfile.h"
#include "hashtabl.h"

pCResMgr
newCResMgr(const String & basepath)
{
	pCResMgr resmgr = new CResMgr;

	if (resmgr)
		CResMgrConstruct(resmgr, basepath);

	return resmgr;
}


void
deleteCResMgr(pCResMgr This)
{
	if (This)
	{
		CResMgrDestruct(This);
		delete This;
		This = NULL;
	}
}


void
CResMgrConstruct(pCResMgr This, const String & basepath)
{
	if (This)
	{
		This->cResFiles = NULL;
		This->cBasePath = basepath;
	}
}


void
CResMgrDestruct(pCResMgr This)
{
	pCResFile	resfile		= This->cResFiles;
	pCResFile	next;


	if (This)
	{
		while (resfile)
		{
			next = resfile->cNext;
			deleteCResFile(resfile);
			resfile = next;
		}
	}
}


uint32
CResMgrOpenResFile(pCResMgr This, const String & Filename)
{
	pCResFile	resfile = NULL;

	if (This && Filename && *Filename)
	{
		String fullpath;
	
		fullpath = This->cBasePath;
		fullpath += Filename;

		resfile = newCResFile(fullpath);

		if (resfile)
		{
			resfile->cNext = This->cResFiles;
			This->cResFiles = resfile;
		}
		else
			DEBUGMSG(("CResMgrOpenResFile(): %S will not open.", (const wchar_t*)Filename));
	}

	return (uint32)resfile;
}


void
CResMgrCloseResFile(pCResMgr This, uint32 ResFileID)
{
	pCResFile	resfile		= NULL;
	pCResFile	prevresfile	= NULL;
	bool8 found = false;

	if (This && ResFileID)
	{
		resfile = This->cResFiles;

		while (resfile)
		{
			if ((pCResFile)ResFileID == resfile)
			{
				found = true;
				break;
			}

			prevresfile	= resfile;
			resfile		= resfile->cNext;	
		}

		if (found && resfile)
		{
			// Update pointers if we closed a resfile.
			if (prevresfile == NULL)
				This->cResFiles = resfile->cNext;
			else
				prevresfile->cNext = resfile->cNext;

			deleteCResFile(resfile);
		}
	}
}


void *
CResMgrGetResource(pCResMgr This, char * ResName, uint32 ResGrp)
{
	pCResFile	resfile	= This->cResFiles;
	void *		data	= NULL;

	while (resfile)
	{
		if ((data = CResFileGetResource(resfile, ResName)) != NULL)
			break;

		resfile = resfile->cNext;	
	}

	return data;
}

void *
CResMgrGetResource(pCResMgr This, const String& ResName, uint32 ResGrp)
{
	uint32 len =ResName.getLength();
	char* szResName = (char*)_alloca(len + 1);

	StringConversions::convert(szResName, ResName, len+1);
	szResName[len] = '\0';

	return CResMgrGetResource(This, szResName, ResGrp);
}

bool8
CResMgrReleaseResource(pCResMgr This, void *ResData)
{
	// We don't really care which ResFile 'owns' the ResData because
	//   the ResData has a reference back to it's ResEntry.  And since
	//   ResEntries are independant from ResFiles (during resource
	//   releasing) we still don't care.  We are just passing this
	//   to ResFile so it can pass it on to ResEntry.  (Just following
	//   the chain of command.)

	if (This && This->cResFiles)
		return CResFileReleaseResource(This->cResFiles, ResData);

	return false;
}
